import requests
from Configs.setting import logger
from urllib.parse import urlparse, parse_qs

def post_request(url, data={}, headers={}):
    """act sync post request with tornado httpclient

    Args:
        url (String): url with parameters
        data (Dict): body data
    """
    try:
        requests.post(url, data=data, headers=headers)
    except Exception as e:
        logger.warning('post request error %s: %s' % (url, e))


def decode_url_parameters(url):
    url = url.replace("&amp;", "&")
    qs_dict = parse_qs(urlparse(url).query)
    d = {}
    for k,v in qs_dict.items():
        d[k] = v[0]
    return d

def dict_to_parameters(data, join_symbol="&", split_symbol="="):
    s = ''
    for k in data:
        s += str(k) + split_symbol + str(data[k]) + join_symbol
    return s[:-1]

if __name__ == '__main__':
    print(decode_url_parameters('?r=123&c=frank'))